define(['store'], function (store) {
    'use strict';
    function ctrl($scope, $state, $stateParams,$ionicLoading, LocalCacheService, CommonService, WebService) {

        function init() {
            $scope.account = {
                accountType: $stateParams.typeid,
                checked: false
            };

            $scope.openAccount = {};

            $scope.user = LocalCacheService.getUser();
            var accountType=$scope.account.accountType;
            var exchange_kind=null;
            if(accountType=='1'){//沪A
                exchange_kind='10';
            }else  if(accountType=='2'){//深A
                exchange_kind='20';
            }
            var params = {client_id:$scope.user.client_id,exchange_kind:exchange_kind};

            $ionicLoading.show();
            WebService.qryStockholderDetail(params).then(function (data) {
                $ionicLoading.hide();
                $scope.openAccount.needVideo=data.hasVideo != 'true';
                $scope.openAccount.status = data.open_status;
                $scope.openAccount.needUploadIDCard=data.hasUploadIDCard!= 'true';
                store.set('needVideo',$scope.openAccount.needVideo);
                store.set('needUploadIDCard',  $scope.openAccount.needUploadIDCard);
                store.set('exchange_kind',  exchange_kind);
                if(data.open_status=='1'){//已开通
                    if(exchange_kind=='10'){
                        if(data.regflag == "1"){
                            $scope.openAccount.subtext="您已有上海A股账户（" + data.stock_account + "）。如有疑问，请拨打客服热线：400-800-5000。";
                        }else if(data.regflag == "0"){
                            if(data.csdc_time_flag == "1"){// 查询是否是交易时间
                                if(data.csdc_relation_flag == "0"){
                                    $scope.openAccount.subtext="您已有上海A股账户（" + data.stock_account + "），但未指定在天风证券，可能是由于您之前提交过新开户或转户申请，系统尚未处理完成。请您在下一交易日查询处理结果，如果未开户或转户成功，可以再次提交申请。如有疑问，请拨打客服热线：400-800-5000。";
                                }else if(data.csdc_relation_flag == "1"){
                                    $scope.openAccount.subtext="您已有上海A股账户（" + data.stock_account + "）,但已指定在其它证券公司。如有疑问，请拨打客服热线：400-800-5000。";
                                }
                            }else if(data.csdc_time_flag == "0"){
                                $scope.openAccount.subtext="您已有上海A股账户（" + data.stock_account + "），但未指定在天风证券，可能是由于您之前提交过新开户或转户申请，系统尚未处理完成。请您在下一交易日查询处理结果，如果未开户或转户成功，可以再次提交申请。如有疑问，请拨打客服热线：400-800-5000。";
                            }
                        }
                    }else if(exchange_kind=='20'){
                        $scope.openAccount.subtext="您已有深圳A股账户（" + data.stock_account + "）。如有疑问，请拨打客服热线：400-800-5000。";
                    }
                }else if(data.open_status=='9'){//处理中 您已经提交了开户申请，系统正在处理中，无需再次提交申请。如有疑问，请拨打客服热线：400-800-5000。
                    $scope.openAccount.subtext="您已经提交了开户申请，系统正在处理中，无需再次提交申请。如有疑问，请拨打客服热线：400-800-5000。";
                }/*else if(data.open_status=='2'){//处理失败
                    CommonService.showConfig({message:"很抱歉，您之前的开户申请处理失败！如要继续开户，请重新提交开户申请。"});
                }*/
            },function(){
                $ionicLoading.hide();
                $scope.openAccount.status='5';
                $scope.openAccount.subtext="获取信息错误";
            });
        }

        $scope.$on('$ionicView.beforeEnter', function () {
            init()
        });

        $scope.submit = function () {
            if ($scope.openAccount.needUploadIDCard) {//跳转到身份验证
                $state.go('tab.account-openAccountId', {typeid: $stateParams.typeid});
            }else {
                if ($scope.openAccount.needVideo) {  //跳转到视频验证
                    $state.go('tab.account-openAccountVideo', {typeid: $stateParams.typeid});
                } else {
                    //提交开户信息
                    var accountType = $scope.account.accountType;
                    var exchange_kind = null;
                    if (accountType == '1') {//沪A
                        exchange_kind = '10';
                    } else if (accountType == '2') {//深A
                        exchange_kind = '20';
                    }
                    var params = {exchange_kind: exchange_kind, sc_open_flag: 1};
                    $ionicLoading.show();
                    WebService.addStockAccount(params).then(function (data) {
                        $ionicLoading.hide();
                        if (data.error_info) {
                            $scope.openAccount.status = 1;
                            $scope.openAccount.subtext = data.error_info;
                        } else {
                            CommonService.showComplete('../view/template/accounttemplate.html', '返回首页', 'tab.account-index');
                        }
                    },function(){
                        $ionicLoading.hide();
                        CommonService.showAlert({message:"提交失败"});
                    });
                }
            }
        };
        //跳转到转户
        $scope.trans = function () {
            $state.go('tab.account-transAccount', {typeid: $stateParams.typeid});
        }
    }

    ctrl.$inject = ['$scope', '$state', '$stateParams', '$ionicLoading','LocalCacheService', 'CommonService', 'WebService'];
    return ctrl;
});